#include <sstream>
#include "certifier.hh"
using namespace std;

typedef pair<fixedBitSizeInteger, fixedBitSizeInteger> integerPair;

certifier::certifier(userModularFunctionBase* f){

    task = f;
    //generating the prime table
    primeTable = generate21bitsprimes(); // should 20 bits primes 
//    cout << "prima  table size = " << primeTable.size() << endl;
    pIndex = primeTable.size();

}

bool certifier::certify(integer* result, size_t primeIndex){
    cout << "[certifier] certifying..." << endl;

    task->userModularFunction(primeTable[primeIndex]);//execute the user function
    

    integerPair* pairptr = reinterpret_cast<integerPair*>(task->getPair(0)); //get the result pair

    stringstream tmp;
    tmp <<  pairptr->first; 
    integer test_res (tmp.str(), 10);// we take the residue

    tmp.str("");
    tmp << pairptr->second;
    integer prime(tmp.str(),10);
    //cout << "result pair is " << pairptr->first << ',' << pairptr->second<< endl;
    integer res = *result % prime;

    cout << "[certifier] test   mod " << prime << " = " << test_res << endl;
    cout << "[certifier] result mod " << prime << " = " <<  res << endl; 
    if(res == test_res){
        cout << "[certifier] certification success! " << endl;
        return true;
    }
    else {

        cout << "[certifier] certification failure! " << endl;
        return false;
    }
}

bool certifier::certify(integer* result){

    pIndex --;
    return certify(result, pIndex);
}
